import rsa
import binascii
import sys
import base64

PY3 = sys.version_info.major == 3
public_key = """MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCPtA4V6NwxmzjT0oAZncHWmU7S
                ZhCG86zOhDHz/Mk+Yp3+uY5kwQFy5DxOXTKU/HBfc9V3J8ucVGmJ3Pk/tAf12bXI
                pZDcRzqbH88mqueA8LlLxW7AbMzYYymUK9lWeH75aG5s0eVrCNloRmxWX/XGqVP+
                vr25ENSS7KbMeFGGZwIDAQAB
                """
pem_public_key = "-----BEGIN PUBLIC KEY-----\n" + public_key + "\n-----END PUBLIC KEY-----"
private_key = """MIICXQIBAAKBgQCPtA4V6NwxmzjT0oAZncHWmU7SZhCG86zOhDHz/Mk+Yp3+uY5k
                wQFy5DxOXTKU/HBfc9V3J8ucVGmJ3Pk/tAf12bXIpZDcRzqbH88mqueA8LlLxW7A
                bMzYYymUK9lWeH75aG5s0eVrCNloRmxWX/XGqVP+vr25ENSS7KbMeFGGZwIDAQAB
                AoGAPSa5ccBgZ2mu4pDquzvjPz+JB8awFMWADXe6zCGPv9D2e+2Zdy2cwLf4I77M
                JhNQfIYN2kFgh1tyd9zFYkLpuaU7AxNhxxqBaQtKgegQTgW5QQMZvzfQ3/Il30XC
                h19zyv/Jfzv+SinaDXhjT5WezvIe37DN2ieBXt86gKwEeXECQQDHIs9wRhlc+sdl
                oEf7/CMAyA3Ht+qiL/W1gMpWKU29sqRcDTT16xN03gvhZCoX+/RpB7IfSf4V3jD6
                BiQEobo3AkEAuL0Fg58NLILCHouzDYnwwv4qIyjDMFVoVuy8Mo68DLWJHP1sjcTj
                kmtdeUnOVvZVH2fAh5b9J+/NVjdVmS29UQJBALkho7i9ctb+TmIsoL+jqOcRFb6X
                PYuLCuOy2GlB/QRfEG6D2LjH007TIDhq5rdlhdQfU3nDUHUEHIRVMScdNAECQQCj
                u3w4pO5DJf0wn3ZpqHlICYaFNOqSWxAoApfmY1VOGlNU61Ob7wYXzqrBBeYocxkM
                xaMBSZREIiimolJuefiRAkAlS+71cItXuyVtHPfnTJhY3VuKB83LTjCXd3vUL7Rc
                arkO6nzqy16v9tflvDSZFKfCvZMtDkFGXkXjOc2dZNL7"""
pem_private_key = "-----BEGIN RSA PRIVATE KEY-----\n" + private_key + "\n-----END RSA PRIVATE KEY-----"


# RSA私钥加密
def rsa_encrypt(encrypt_text):
    if PY3:
        b_pem_private_key = pem_private_key.encode('utf-8')
        key = rsa.PrivateKey.load_pkcs1(b_pem_private_key)
    else:
        key = rsa.PrivateKey.load_pkcs1(pem_private_key)

    d = key.d
    n = key.n
    block_size = int(key.n.bit_length() / 8 - 11)

    keylength = int(key.n.bit_length() / 8)
    encrypt_text = base64.b64encode(str(encrypt_text).encode("utf-8"))
    out_data = bytes()

    while encrypt_text:
        input_data = encrypt_text[:block_size]
        encrypt_text = encrypt_text[block_size:]
        input_data = rsa.pkcs1._pad_for_signing(input_data, keylength)
        num = rsa.transform.bytes2int(input_data)
        encrypto = rsa.core.encrypt_int(num, d, n)
        out_data += rsa.transform.int2bytes(encrypto)
    return out_data.hex()


# RSA公钥加密
def rsa_decrypt(decrypt_text):
    if PY3:
        b_pem_public_key = pem_public_key.encode('utf-8')
        key = rsa.PublicKey.load_pkcs1_openssl_pem(b_pem_public_key)
    else:
        key = rsa.PublicKey.load_pkcs1_openssl_pem(pem_public_key)

    e = key.e
    n = key.n
    block_size = int(key.n.bit_length() / 8)
    decrypt_text = binascii.unhexlify(decrypt_text)
    out_data = bytes()
    while decrypt_text:
        input_data = decrypt_text[:block_size]
        decrypt_text = decrypt_text[block_size:]
        num = rsa.transform.bytes2int(input_data)
        decrypto = rsa.core.decrypt_int(num, e, n)
        out_data += rsa.transform.int2bytes(decrypto)
    return str(base64.b64decode(out_data), encoding="utf-8")


en = rsa_encrypt('Hello World!!!Hello World!!!Hello World!!!Hello World!!!Hello World!!!Hello World!!!Hello World!!!')
print("加密内容：{}".format(en))
print("解密内容：{}".format(rsa_decrypt(en)))